package com.mutouren.web.ca;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.mutouren.common.entity.PageInfo;
import com.mutouren.common.entity.Result;
import com.mutouren.common.entity.ValidState;
import com.mutouren.common.utils.StringUtils;
import com.mutouren.common.web.BootStrapPage;
import com.mutouren.common.web.ParsePageInfo;
import com.mutouren.modules.ca.model.User;
import com.mutouren.modules.ca.service.UserService;
import com.mutouren.web.WebUtils;

@Controller
@RequestMapping(value="/ca")
public class UserAction {
	
	@Resource
	private UserService userService;	
	
	@RequestMapping(value="/userManage.action")
	public String userManage(HttpServletRequest request) {		
		PageInfo pageInfo = ParsePageInfo.parse(request);
		
		List<User> listUser = userService.select(null, pageInfo);
		request.setAttribute("listUser", listUser);
		
		request.setAttribute("mtrPage", BootStrapPage.buildHtmlPage(pageInfo));		
		
		return "modules/ca/userList";
	}
	
	@RequestMapping(value="/userAdd.action")
	public String userAdd(@ModelAttribute("user") User user, 
			HttpServletRequest request, HttpServletResponse response) {
		
		request.setAttribute("actionName", WebUtils.getActionName(request));		
		if (!WebUtils.isPost(request)) {		
			return "modules/ca/userForm";
		}
		
		List<String> errors = new ArrayList<String>();
		if(this.validate(request, user, errors)) {
			return WebUtils.failDialog(response, WebUtils.getErrorMessage(errors));
		}
		
		Result result = userService.register(user);
		if (result.getCode() == 0) {
			return WebUtils.successDialog(response);
		} else {
			return WebUtils.failDialog(response, "用户名已存在，请重新命名");
		}
	}
	
	@RequestMapping(value="/userModify.action")
	public String userModify(
			@ModelAttribute("user") User user,
			@RequestParam(value="userId") long userId,
			ModelMap map,
			HttpServletRequest request, HttpServletResponse response) {
		
		request.setAttribute("actionName", WebUtils.getActionName(request));
		if (!WebUtils.isPost(request)) {
			user = userService.get(userId);
			map.addAttribute("user", user);
			return "modules/ca/userForm";
		}
		
		List<String> errors = new ArrayList<String>();
		if(this.validate(request, user, errors)) {
			return WebUtils.failDialog(response, WebUtils.getErrorMessage(errors));
		}		
		
		int result = userService.modify(user);
		if (result == 1) {
			return WebUtils.successDialog(response);
		} else {
			return WebUtils.failDialog(response, "编辑数据不存在");
		}				
	}	
	
	@RequestMapping(value="/changeState.action")
	public void changeState(@RequestParam(value="userId") long userId, String validState,
			HttpServletResponse response) throws IOException {
		User paramUser = new User();
		paramUser.setUserId(userId);
		paramUser.setValidState(ValidState.valueOf(validState));
		
		if (userService.setState(paramUser) == 1) {
			response.getWriter().write("success");
		} else {
			response.getWriter().write("fail");
		}
	}
	
	@RequestMapping(value="/resetPassword.action")
	public void resetPassword(@RequestParam(value="userId") long userId, 
			HttpServletResponse response) throws IOException{
		
		userService.resetPassword(userId);
		response.getWriter().write("success");
	}	
	
	public boolean validate(HttpServletRequest request, Object model, List<String> errors ) {
		String actionName = (String) request.getAttribute("actionName");

		// user
		if (actionName.equals("userAdd") || actionName.equals("userModify")) {
			User user = (User) model;
			if (StringUtils.isBlank(user.getUserName())) {
				errors.add("用户名: 不能为空");
			}
			if (StringUtils.isBlank(user.getEmail())) {
				errors.add("邮箱: 不能为空");
			}			
		}

		return errors.size() > 0;
	}	

}
